package com.grpcserver.service;

import com.grpcclient.goods.GoodsGrpc;
import com.grpcclient.goods.GoodsId;
import com.grpcclient.goods.GoodsInfo;
import com.grpcclient.goods.GoodsInfoList;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: LCG
 * @date: 2022-10-26 13:58:01
 * @description:
 **/
@GrpcService
@Slf4j
public class GrpcGoodsService extends GoodsGrpc.GoodsImplBase {

    @Override
    public void getGoodsList(GoodsId request, StreamObserver<GoodsInfoList> responseObserver) {

        log.info("请求参数--={}",request);
        List<GoodsInfo> listGoods=new ArrayList<>();
        for (int i=0;i<10;i++){
            GoodsInfo.Builder builder = GoodsInfo.newBuilder().setGoodsId(i)
                    .setGoodsname("商品名称：" + i)
                    .setProductname("品类名称：" + i);
            listGoods.add(builder.build());
        }

        GoodsInfoList build = GoodsInfoList.newBuilder().addAllGoodsInfo(listGoods).build();

        responseObserver.onNext(build);
        responseObserver.onCompleted();

    }
}
